package com.xframework.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class BusinessException extends RuntimeException {
    private static final long serialVersionUID = 1L;
    private static Logger logger = LoggerFactory.getLogger(BusinessException.class);
    private static InetAddress localAddress = null;
    private static String localIp = null;
    private static String MESSAGE = "${message}";

    static {
        try {
            localAddress = InetAddress.getLocalHost();
            localIp = "{\"localIp\":\"" + localAddress.getHostAddress() + "\",\"applicationName\":\"" + ExceptionCode.APPLICATION_NAME + "\",\"message\":\"" + MESSAGE + "\"}";
        } catch (UnknownHostException e) {
            logger.error(null, e);
        }
    }

    public BusinessException() {
        super();
    }

    public BusinessException(String message) {
        super(message);
        logger.error(localIp.replace(MESSAGE, message));
    }

    public BusinessException(Throwable e) {
        super(e);
        logger.error(localIp.replace(MESSAGE, e.getMessage()), e);
    }

    public BusinessException(String message, Throwable e) {
        super(message, e);
        logger.error(localIp.replace(MESSAGE, message), e);
    }
}
